package com.ruoyi.crm.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.crm.domain.CrmCustomer;
import com.ruoyi.crm.domain.CrmOrder;
import com.ruoyi.crm.mapper.CrmCustomerMapper;
import com.ruoyi.crm.mapper.CrmOrderMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.CrmCostMapper;
import com.ruoyi.crm.domain.CrmCost;
import com.ruoyi.crm.service.ICrmCostService;

/**
 * 费用管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-04
 */
@Service
public class CrmCostServiceImpl extends ServiceImpl<CrmCostMapper,CrmCost> implements ICrmCostService {
    @Autowired
    private CrmCostMapper crmCostMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private CrmOrderMapper crmOrderMapper;

    @Autowired
    private CrmCustomerMapper crmCustomerMapper;

    /**
     * 查询费用管理列表
     *
     * @param crmCost 费用管理
     * @return 费用管理
     */
    @Override
    public List<CrmCost> selectCrmCostList(CrmCost crmCost) {
        List<CrmCost> crmCosts = crmCostMapper.selectCrmCostList(crmCost);
        crmCosts.forEach(item->{
            if(StringUtils.isNotEmpty(item.getApplyDeptId())){
                SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(item.getApplyDeptId()));
                item.setApplyDeptName(sysDept.getDeptName());
            }
            if(StringUtils.isNotEmpty(item.getApplyUserId())){
                SysUser sysUser = sysUserMapper.selectUserById(item.getApplyUserId());
                item.setApplyUserName(sysUser.getUserName());
            }
            if(StringUtils.isNotEmpty(item.getOrderId())){
                CrmOrder order = crmOrderMapper.selectById(item.getOrderId());
                item.setOrderName(order.getOrderName());
            }
            if(StringUtils.isNotEmpty(item.getCustomerId())){
                CrmCustomer customer = crmCustomerMapper.selectById(item.getCustomerId());
                item.setCustomerName(customer.getCustomerName());
            }
        });
        return crmCosts;
    }


}
